# -*- coding: utf-8 -*-
# @File: hsJobStart.py
# @Author: Liccsu
# @Time: 12/8/2022 下午11:03
# @Project: eschool-health
import os

from apscheduler.schedulers.background import BlockingScheduler

from src import CronTriggerPlus, Cfg, sm, Task


class Schedule:
    Scheduler = BlockingScheduler()

    @staticmethod
    def start():
        try:
            Schedule.Scheduler.add_job(func=Task.time_task,
                                       trigger=CronTriggerPlus.plus_from_crontab(Cfg.cron_tab, 'Asia/Shanghai'))
            with open(os.path.join(os.getcwd(), '../.pid'), 'w') as f:
                # print(f'\npid: {os.getpid()}')
                f.write(str(os.getpid()))
            sm.create_schedule_success()
            Schedule.Scheduler.start()
            return os.getpid()
        except Exception as e:
            # print(e)
            sm.send_error(e)
            return e


if __name__ == '__main__':
    Schedule.start()
